package com.ryder.petmatediarybackend.common.config.web;

import cn.dev33.satoken.stp.StpInterface;
import com.ryder.petmatediarybackend.module.auth.service.SysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 描述: 自定义权限验证接口扩展（Sa-Token 双 Token 模式）
 *
 * @author Ryder
 * @version 1.1
 * @since 1.0
 * 更新日期: 2025/10/27
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    private final SysRolePermissionService sysRolePermissionService;

    public StpInterfaceImpl(SysRolePermissionService sysRolePermissionService) {
        this.sysRolePermissionService = sysRolePermissionService;
    }

    /**
     * 返回指定用户拥有的权限编码列表
     * Sa-Token 会自动调用此方法进行权限校验
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        Long userId = Long.parseLong(loginId.toString());
        return sysRolePermissionService.getUserPermissions(userId);
    }

    /**
     * 返回指定用户拥有的角色编码列表
     * Sa-Token 会自动调用此方法进行角色校验
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Long userId = Long.parseLong(loginId.toString());
        return sysRolePermissionService.getUserRoles(userId);
    }
}